<?php
namespace app\controller;

class Error
{
    public function _empty() {
        echo '<p><b>特殊参数格式说明</b>: ';
        echo '<br/>Array1:[param1,param2...]';
        echo '<br/>Array2:{"key1":param1,"key2":param2,...}';
        echo '<br/>Array3：param=-key1:value1,key2:value2...，如：page_arr=-p:1,p_rows:20,sort:id desc，仅一维数组';
        echo '<br/>Function: param=:func(param1,param2)，如：end_time=:strtotime(2017-12-15)';
        echo '</p>';
        
        $controller_name    = request()->controller();
        $action_name        = request()->action(true);
        
        if (class_exists('\\app\\event\\'.$controller_name)) {
            $event  = controller($controller_name, 'event');
            if (method_exists($event, $action_name)) {
                $is_exe = true;
                $event->{$action_name}();
            }
        }
        if (!$is_exe) {
            
            //自动接口调用
            $controller_name    = '\\NMClient\\Api\\'.$controller_name;
            if (!class_exists($controller_name)) {
                exit('API:`'.$controller_name.'` not exists');
            }
            echo '<h2>Api -> get.act: '.implode(',', get_class_methods($controller_name)).'</h2><hr/><br/>';
            if (!method_exists($controller_name, $action_name)) {
                exit('`'.$controller_name.'::'.$action_name.'` not exists');
            }
            
            $Reflection    = new \ReflectionMethod($controller_name, $action_name);
            $params = $Reflection->getParameters();
            foreach ($params as $key => $param) {
                $data[$param->name] = input('get.'.$param->name);
                // array
                switch ($data[$param->name][0]) {
                    case '-':
                        if (strstr($data[$param->name], ':')) {
                            $arr    = explode(',', trim($data[$param->name],'-'));
                            $data[$param->name] = null;
                            foreach ($arr as $value) {
                                $value  = explode(':', $value, 2);
                                $data[$param->name][$value[0]]   = $value[1];
                            }
                        }
                        break;
                    case ':':
                        $arr    = explode('(', trim($data[$param->name],':'), 2);
                        $func   = $arr[0];
                        $func_param = explode(',', rtrim($arr[1], ')'));
                        $data[$param->name] = call_user_func_array($func, $func_param);
                        break;
                    case '[':
                    case '{':
                        $data[$param->name] = str_replace('&quot;', '"', $data[$param->name]);
                        $data[$param->name] = json_decode($data[$param->name], true);
                }
            }
            
            if (key_exists('access_token', $data)) {
                $dc_token   = validateDcToken();
                
                $data['access_token']   = $dc_token['access_token'];
            }
            echo '<p>param -> get.'.implode(',', array_keys($data)).'</p><hr/>';
            $res = forward_static_call_array([$controller_name,$action_name], $data);
        }
        
        $res = \NMClient\Core\Http::requestInfo();
        if (is_array($res['response'])) {
            dump($res);
        } else {
            $response   = $res['response'];
            unset($res['response']);
            dump($res);
            echo $response;
        }
    }
}